﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Client
{
    public partial class LoginForm : Form
    {
        private SubscribeForm subscribeForm;

        public LoginForm(MainForm mainForm)
        {
            InitializeComponent();
            mainForm.Hide();
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            ConnectToServer();
        }

        private void ConnectToServer()
        {
            try
            {
                ClientManager.Instance.Connect();
            }
            catch (ConnectionException ex)
            {
                DialogResult serverMessage = MessageBox.Show(ex.Message + "\nPlease verify server connection.\n\nClick OK button to reconnect or Cancel button to exit.", "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                if (serverMessage == DialogResult.OK)
                {
                    ConnectToServer();
                }
                if (serverMessage == DialogResult.Cancel)
                {
                    System.Environment.Exit(System.Environment.ExitCode);
                }
            }
        }

        private void loginButton_Click(object sender, EventArgs e)
        {
            string username = usernameTextBox.Text;
            try
            {
                ClientManager.Instance.LoginUser(username, passwordTextBox.Text);
                ClientManager.Instance.GetCourseList(username);
                ClientManager.Instance.GetQuestionList(username);
            }
            catch (ConnectionException ex)
            {
                MessageBox.Show(ex.Message + "\nProgram will exit", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                System.Environment.Exit(System.Environment.ExitCode);
            }
        }

        private void usernameTextBox_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (!Regex.IsMatch(usernameTextBox.Text, @"^\s*$") && !Regex.IsMatch(usernameTextBox.Text, @"^[a-zA-Z'./s]{1,10}$"))
                {
                    throw (new ValidationException(usernameTextBox, "Username should be up to 10 characters"));
                }
            }
            catch (ValidationException ex)
            {
                MessageBox.Show(ex.Message);
                ex.BadTextBox.Focus();
                ex.BadTextBox.Clear();
            }
        }

        private void passwordTextBox_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (!Regex.IsMatch(passwordTextBox.Text, @"^\s*$") && !Regex.IsMatch(passwordTextBox.Text, @"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{3,10}$"))
                {
                    throw (new ValidationException(passwordTextBox, "Password should be at least 4 characters, no more than 8 characters, and must include at least one upper case letter, one lower case letter, and one numeric digit"));
                }
            }
            catch (ValidationException ex)
            {
                MessageBox.Show(ex.Message);
                ex.BadTextBox.Focus();
                ex.BadTextBox.Clear();
            }
        }

        private void registerButton_Click(object sender, EventArgs e)
        {
            subscribeForm = new SubscribeForm();
            subscribeForm.ShowDialog();
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Environment.Exit(System.Environment.ExitCode);
        }

        public void CloseSubscribeFrom()
        {
            subscribeForm.Close();
        }
    }
}
